package com.zzptc.gyc.myapp2;

import android.graphics.Color;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class SecondFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View root=inflater.inflate(R.layout.fragment_second, container, false);
        BarChart bar=root.findViewById(R.id.bar_Chart);
        List<BarEntry>barEntryList=new ArrayList<>();
        List<BarEntry>mbarEntryList=new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            barEntryList.add(new BarEntry(i,new Random().nextInt(10)));
            mbarEntryList.add(new BarEntry(i,new Random().nextInt(20)));
        }
        BarDataSet barDataSet=new BarDataSet(barEntryList,"男");
        barDataSet.setColor(Color.RED);
        BarDataSet barDataSet1=new BarDataSet(mbarEntryList,"女");
        barDataSet1.setColor(Color.BLUE);
        BarData barData=new BarData(barDataSet);
        barData.addDataSet(barDataSet1);
        bar.setData(barData);
        //获取x轴，将x轴移到下面
        bar.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
        //隐藏右边y轴
        bar.getAxisRight().setEnabled(false);
        barData.setBarWidth(0.2f);
        //这个代码有顺序
        bar.groupBars(1,0.1f,0);

        bar.getXAxis().setAxisMaximum(6);
        bar.getXAxis().setAxisMinimum(1);
        //数据不完整的处理
        bar.getXAxis().setLabelCount(5,false);
        bar.getXAxis().setCenterAxisLabels(true);
        return root;
    }

}
